package net.chathu.airgw.ui 
{
	import flash.desktop.DockIcon;
	import flash.desktop.Icon;
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindow;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ScreenMouseEvent;
	import flash.net.URLRequest;
	import mx.controls.Image;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.Debug;
	/**
	 * ...
	 * @author ze
	 */
	public class UITest 
	{

		[Embed(source="../../../../../assets/images/icon/gw_logo_exp_16.png")]
		
		private var iconClass:Class;
		private var icon:Loader = new Loader();
		private var nativeWindow:NativeWindow;
		public function UITest(nativeWindow:NativeWindow) 
		{
			/*var req:URLRequest = new URLRequest(value.file.url);
			var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			ldr.load(req);*/
			this.nativeWindow = nativeWindow;
			
			if (NativeApplication.supportsDockIcon) {
				//var dockIcon:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;
				//NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, undock);
				//dockIcon.menu = createIconMenu();
				//Mac OS support(?!)
			} else if (NativeApplication.supportsSystemTrayIcon){
				var sysTrayIcon:SystemTrayIcon =
				NativeApplication.nativeApplication.icon as SystemTrayIcon;
				sysTrayIcon.tooltip = Config.PROGRAM_NAME;
				//sysTrayIcon.addEventListener(MouseEvent.CLICK,undock);
				sysTrayIcon.menu = createIconMenu();
				sysTrayIcon.addEventListener(ScreenMouseEvent.CLICK, onTrayIconClick);
				icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
				var img:* = new iconClass();
				NativeApplication.nativeApplication.icon.bitmaps=[img.bitmapData];
			}
		}
		
		 private function iconLoadComplete(event:Event):void
        {
            NativeApplication.nativeApplication.icon.bitmaps =[event.target.content.bitmapData];
        }
		
		private var optionsCommand:NativeMenuItem = new NativeMenuItem("Beállítások");
		private var infosCommand:NativeMenuItem = new NativeMenuItem("Információk");
		private var helpCommand: NativeMenuItem = new NativeMenuItem("Súgó");
		 
		private function createIconMenu():NativeMenu {
			
			var iconMenu:NativeMenu = new NativeMenu();
			if(NativeApplication.supportsSystemTrayIcon){
				//iconMenu.addEventListener(Event.SELECT, onMenuSelect);
				//iconMenu.addItem(optionsCommand);
				//iconMenu.addItem(infosCommand);
				//iconMenu.addItem(new NativeMenuItem("", true));//Separator
				
				//iconMenu.addItem(helpCommand);
				//helpCommand.addEventListener(Event.SELECT, undock);
				//iconMenu.addItem(new NativeMenuItem("", true));//Separator
				var exitCommand: NativeMenuItem = iconMenu.addItem(new NativeMenuItem("Kilépés"));
				exitCommand.addEventListener(Event.SELECT, exit);
			}
			//iconMenu.addItem(new NativeMenuItem("", true));//Separator
			return iconMenu;
		}
		
		
		private function onTrayIconClick(event:Event):void {
			nativeWindow.visible = true;
			nativeWindow.activate();
		}
		
		private function onMenuSelect(event:Event):void {
			//Debug.echo("Menu selected: " + event.target);
		}
		
		public function dock(event:Event = null):void{
			//stage.nativeWindow.visible = false;
			//NativeApplication.nativeApplication.icon.bitmaps = [clock.bitmapData];
		}

		public function undock(event:Event = null):void{
			//stage.nativeWindow.visible = true;
			//NativeApplication.nativeApplication.icon.bitmaps = [];
		}
		
		private function exit(event:Event):void {
			NativeApplication.nativeApplication.exit();
		}
		
	}

}